﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Configuration;

public partial class SiteWorx_SiteManager : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            LoadThemes();
    }

    protected void cdsPageList_SelectObject(object sender, Csla.Web.SelectObjectArgs e)
    {
        e.BusinessObject = Asah.SiteWorx.SiteMap.PageList.GetPageList();
    }

    protected void rgvPages_DeleteCommand(object source, GridCommandEventArgs e)
    {
        int pageId = int.Parse(((HiddenField)e.Item.FindControl("hidPageId")).Value);
        Asah.SiteWorx.SiteMap.Page page = Asah.SiteWorx.SiteMap.Page.GetPage(pageId);
        if (Asah.SiteWorx.SiteMap.Page.CanDeletePage(page.FileName))
        {
            Asah.SiteWorx.SiteMap.Page.DeletePage(page.ID, page.Url);
            rgvPages.DataBind();
        }
            
    }

    protected void rgvPages_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item.ItemType == GridItemType.AlternatingItem || e.Item.ItemType == GridItemType.Item ||
            e.Item.ItemType == GridItemType.SelectedItem)
        {
            string pageFileName = ((HiddenField)e.Item.FindControl("hidPageFileName")).Value.ToString();
            if (!Asah.SiteWorx.SiteMap.Page.CanDeletePage(pageFileName))
            {
                try
                {
                    ((Button)e.Item.Controls[8].Controls[0]).Enabled = false;
                }
                catch { }
            }
        }
            
            

    }

    protected void rgvPages_EditCommand(object source, GridCommandEventArgs e)
    {
        Response.Redirect("../" + e.CommandArgument, true);
    }

    private void LoadThemes()
    {
        RadToolBarDropDown themeDropButton = (RadToolBarDropDown)rtbPages.FindItemByText("Site Theme");
        if (ConfigurationManager.AppSettings["AvailableThemes"] != null &&
            ConfigurationManager.AppSettings["AvailableThemes"].ToString().Length > 0)
        {
            char[] splitter = { ',' };
            string[] availableThemes = ConfigurationManager.AppSettings["AvailableThemes"].ToString().Split(splitter);
            foreach (string availableTheme in availableThemes)
               themeDropButton.Buttons.Add(new RadToolBarButton(availableTheme));
        }
        else
            themeDropButton.Buttons.Add(new RadToolBarButton("Default"));
    }
    
    protected void rtbPages_ButtonClick(object sender, RadToolBarEventArgs e)
    {
        Asah.SiteWorx.Site.GetSite().ApplyTheme(e.Item.Text);
    }
   
}